home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / timidsrc.zip / dec_a.c < prev    next >
C/C++ Source or Header  |  1996-05-21  |  7KB  |  294 lines

  1. /* 
  2.  
  3.     TiMidity -- Experimental MIDI to WAVE converter
  4.     Copyright (C) 1995 Tuukka Toivonen <toivonen@clinet.fi>
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     dec_audio.c
  21.  
  22.     Functions to play sound on DEC OSF/1 with MME support
  23.  
  24.     Ported by Chi Ming HUNG <cmhung@insti.physics.sunysb.edu>
  25.  
  26.     Note: This was tested on a DEC alpha 250 4/266.  At the default sampling
  27.     rate of 32kHz an occasional break in playing could be detected under
  28.     CPU load of around 3.0  Try using lower sampling rate or using some of
  29.     the hacks in config.h if you have a slow and/or busy machine.
  30.  
  31.     Oh timidity works great as a MIDI-"viewer" for WWW browsers.  Just add
  32.  
  33.       audio/midi; timidity1 %s >/dev/null 2>&1
  34.  
  35.     to your .mailcap file and you can click and play MIDI files on the net!
  36.     Try e.g. the Classical MIDI Archive:
  37.  
  38.       http://www.prs.net/midi.html
  39.  
  40.     Thanks Tuukka for a great program!!
  41. */
  42.  
  43. #include <unistd.h>
  44. #include <fcntl.h>
  45. #include <stdio.h>
  46. #include <sys/ioctl.h> 
  47.  
  48. #include <mme/mme_api.h>
  49.  
  50. #include "config.h"
  51. #include "output.h"
  52. #include "controls.h"
  53.  
  54. static int open_output(void); /* 0=success, 1=warning, -1=fatal error */
  55. static void close_output(void);
  56. static void output_data(int32 *buf, int32 count);
  57. static void flush_output(void);
  58. static void purge_output(void);
  59.  
  60. /* export the playback mode */
  61.  
  62. #define dpm dec_play_mode
  63.  
  64. PlayMode dpm = {
  65.   DEFAULT_RATE, PE_16BIT|PE_SIGNED,
  66.   -1,
  67.   {0,0,0,0,0}, /* no extra parameters so far */
  68.   "DEC audio device", 'd',
  69.   "",
  70.   open_output,
  71.   close_output,
  72.   output_data,
  73.   flush_output,
  74.   purge_output  
  75. };
  76.  
  77. /* Global variables */
  78.  
  79. static HWAVEOUT        mms_device_handle = 0;
  80. static UINT        mms_device_id = 0;
  81. static LPWAVEHDR    mms_lpWaveHeader;
  82.  
  83.  
  84. /********** MMS_Audio_Init **********************
  85.   This just does some generic initialization.  The actual audio
  86.   output device open is done in the Audio_On routine so that we
  87.   can get a device which matches the requested sample rate & format
  88.  *****/
  89.  
  90. void MMS_Audio_Init()
  91. {
  92.   MMRESULT    status;
  93.   LPWAVEOUTCAPS    lpCaps;
  94.  
  95.  
  96.   if( waveOutGetNumDevs() < 1 ) 
  97.   {
  98.     fprintf(stderr,"Audio disabled - No Multimedia Services compatible audio devices available\n");
  99.     return;
  100.   }
  101.  
  102.   /* Figure out device capabilities  - Just use device 0 for now */
  103.  
  104.   if((lpCaps = (LPWAVEOUTCAPS)mmeAllocMem(sizeof(WAVEOUTCAPS))) == NULL ) {
  105.       fprintf(stderr,"Failed to allocate WAVEOUTCAPS struct\n");
  106.       return;
  107.   }
  108.   status = waveOutGetDevCaps( 0, lpCaps, sizeof(WAVEOUTCAPS));
  109.   if( status != MMSYSERR_NOERROR ) {
  110.       fprintf(stderr,"waveOutGetDevCaps failed - status = %d\n", status);
  111.   }
  112.  
  113.   mmeFreeMem(lpCaps);
  114.  
  115. }
  116.  
  117.  
  118. /*********  MMS_wave_callback  ****************
  119.  * This is only used so that we know the audio device has finished
  120.  * playing one block of data and is ready to accept the next
  121.  **********/
  122.  
  123. static void MMS_wave_callback(hWaveOut,wMsg,dwInstance,lParam1,lParam2)
  124. HANDLE hWaveOut;
  125. UINT wMsg;
  126. DWORD dwInstance;
  127. LPARAM lParam1;
  128. LPARAM lParam2;
  129. {
  130.  
  131.    switch(wMsg)
  132.      {
  133.      case WOM_OPEN:
  134.      case WOM_CLOSE: {
  135.        break;
  136.      }
  137.      case WOM_DONE: {
  138.        break;
  139.      }
  140.      default: {
  141.        fprintf(stderr,
  142.            "Unknown MMS waveOut callback messages receieved.\n");
  143.        break;
  144.      }
  145.      }
  146. }
  147.  
  148.  
  149.  
  150. /********** MMS_Audio_On **********************
  151.  * Turn On Audio Stream.
  152.  *
  153.  *****/
  154.  
  155. void MMS_Audio_On()
  156. {
  157.   MMRESULT status;
  158.   LPPCMWAVEFORMAT lpWaveFormat;
  159.  
  160.   /* Setting up the parameters for audio device */
  161.   if((lpWaveFormat = (LPPCMWAVEFORMAT)
  162.       mmeAllocMem(sizeof(PCMWAVEFORMAT))) == NULL ) 
  163.     {
  164.       fprintf(stderr,"Failed to allocate PCMWAVEFORMAT struct\n");
  165.       return;
  166.     }
  167.     lpWaveFormat->wf.nSamplesPerSec = dpm.rate;
  168.     lpWaveFormat->wf.nChannels = 2;
  169.     lpWaveFormat->wBitsPerSample = 16;
  170.     lpWaveFormat->wf.wFormatTag = WAVE_FORMAT_PCM; 
  171.     /*    lpWaveFormat->wf.nBlockAlign = lpWaveFormat->wf.nChannels *
  172.      * ((lpWaveFormat->wBitsPerSample+7)/8) ;   
  173.      *lpWaveFormat->wf.nAvgBytesPerSec = lpWaveFormat->wf.nBlockAlign *
  174.      * lpWaveFormat->wf.nSamplesPerSec ; */
  175.     
  176.     /* Open the audio device in the appropriate rate/format */
  177.     mms_device_handle = 0;
  178.     status = waveOutOpen( &mms_device_handle,
  179.               WAVE_MAPPER,
  180.               (LPWAVEFORMAT)lpWaveFormat,
  181.               MMS_wave_callback,
  182.               NULL,
  183.               WAVE_ALLOWSYNC | CALLBACK_FUNCTION  );
  184.  
  185.     if( status != MMSYSERR_NOERROR ) {
  186.       fprintf(stderr,"waveOutOpen failed - status = %d\n", status);
  187.     }
  188.  
  189.     fprintf(stderr,"Opened waveOut device #%d \n",
  190.         mms_device_id);
  191.     fprintf(stderr,
  192.         "Format=%d, Rate=%d, channels=%d, bps=%d \n",
  193.         lpWaveFormat->wf.wFormatTag,
  194.         lpWaveFormat->wf.nSamplesPerSec,
  195.         lpWaveFormat->wf.nChannels,
  196.         lpWaveFormat->wBitsPerSample );
  197.     
  198.     mmeFreeMem(lpWaveFormat);
  199.  
  200.     /* Allocate wave header for use in write */
  201.     if((mms_lpWaveHeader = (LPWAVEHDR)
  202.     mmeAllocMem(sizeof(WAVEHDR))) == NULL ) 
  203.       {
  204.     fprintf(stderr,"Failed to allocate WAVEHDR struct\n");
  205.     return;
  206.       }
  207.  
  208.     /* Allocate shared audio buffer for communicating with audio device */
  209.     if ( (mms_lpWaveHeader->lpData = (LPSTR)
  210.       mmeAllocMem(1024*8 )) == NULL)
  211.       {
  212.     fprintf(stderr,"Failed to allocate shared audio buffer\n");
  213.     return;
  214.       }
  215.  
  216. }
  217.  
  218.  
  219.  
  220. /* Open the audio device */
  221.  
  222. static int open_output(void)
  223. {
  224.   int warnings=0;
  225.  
  226.   MMS_Audio_Init();
  227.   MMS_Audio_On();
  228.   
  229.   return warnings;
  230. }
  231.  
  232.  
  233.  
  234. /* Output of audio data from timidity */
  235.  
  236. static void output_data(int32 *buf, int32 count)
  237. {
  238.  
  239.   MMRESULT    status;
  240.  
  241.   if (!(dpm.encoding & PE_MONO)) count*=2; /* Stereo samples */
  242.  
  243.   /* Convert data to signed 16-bit PCM */
  244.   s32tos16l(buf, count);
  245.  
  246.   /* write output data to audio device */
  247.   mms_lpWaveHeader->dwBufferLength = 2*count;
  248.   memcpy( mms_lpWaveHeader->lpData, buf, 2*count);
  249.   status = waveOutWrite(mms_device_handle, mms_lpWaveHeader,
  250.             sizeof(WAVEHDR));
  251.   if( status != MMSYSERR_NOERROR ) 
  252.     {
  253.       fprintf(stderr,"waveOutWrite failed - status = %d\n",status);
  254.     }
  255.  
  256.   /* Wait for callback from audio device before continuing */
  257.   mmeWaitForCallbacks();
  258.   mmeProcessCallbacks();
  259.  
  260. }
  261.  
  262.  
  263. /* close output device */
  264. static void close_output(void)
  265. {
  266.   MMRESULT status;
  267.  
  268.   status = waveOutReset(mms_device_handle);
  269.   if( status != MMSYSERR_NOERROR ) {
  270.       fprintf(stderr,"waveOutReset failed - status = %d\n", status);
  271.   }
  272.   status = waveOutClose(mms_device_handle);
  273.   if( status != MMSYSERR_NOERROR ) {
  274.       fprintf(stderr,"waveOutClose failed - status = %d\n", status);
  275.   }
  276.  
  277.   mmeFreeMem(mms_lpWaveHeader);
  278.  
  279. }
  280.  
  281.  
  282. /* not sure what to do here */
  283. static void flush_output(void) {}
  284.  
  285. static void purge_output(void)
  286. {
  287.   MMRESULT status;
  288.  
  289.   status = waveOutReset(mms_device_handle);
  290.   if( status != MMSYSERR_NOERROR ) {
  291.     fprintf(stderr,"waveOutReset failed - status = %d\n", status);
  292.  }
  293.